﻿using Common.Library.HTTP;
using Spider.DB;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace Spider.Get
{
    /// <summary>
    /// 获取页面信息，只负责获取页面
    /// </summary>
    public class GetHTML : GetBase, IGetRequest
    {
        static IDBs db { get; set; }
        static int depthconst = 5;
        #region Get 获取页面信息
        /// <summary>
        /// Get 获取页面信息
        /// </summary>
        /// <param name="url"></param>
        /// <param name="depth"></param>
        /// <returns></returns>
        public static string Get(string url, int depth = 1, string cookie = "")
        {
            string html = "";
            try
            {
                html = SpiderClientHepler.ChromeGet(url, cookie);
            }
            catch (Exception ex)
            {
                #region 页面抓取异常，写入日志
                try
                {
                    if (db == null)
                    {
                        db = new MongoDB持久化();
                    }

                    db.Save<ErrorLog>(new ErrorLog()
                    {
                        depth = depth,
                        ErrorMsg = ex.Message,
                        Url = url,
                        InnerException = ex.InnerException.Message
                    });
                }
                catch
                { }
                #endregion

                if (depth < depthconst)
                {
                    Thread.Sleep(2000);
                    html = Get(url, ++depth, cookie);
                }
            }
            if (string.IsNullOrEmpty(html))
            {
                if (depth < depthconst)
                {
                    html = Get(url, ++depth, cookie);
                }
            }
            return html;
        }
        #endregion
    }

    public class ErrorLog
    {
        public string Url { get; set; }
        public int depth { get; set; }
        public string ErrorMsg { get; set; }
        public string InnerException { get; set; }
    }

}
